<template>
  <div class="custom-theme">
    <el-dialog title="新增文章" :visible="dialogVisible" @close="closeFn">
      <span>
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px">
          <el-form-item label="文章标题" prop="title">
            <el-input v-model="ruleForm.title"></el-input>
          </el-form-item>
          <el-form-item label="文章内容" prop="articleBody">
            <quill-editor v-model="ruleForm.articleBody"  :options="editorOption"></quill-editor>
          </el-form-item>
          <el-form-item label="视频地址">
            <el-input v-model="ruleForm.videoURL"></el-input>
          </el-form-item>
        </el-form>
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="quFn">取 消</el-button>
        <el-button type="primary" @click="queFn">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { quillEditor } from 'vue-quill-editor'
import 'quill/dist/quill.snow.css'
export default {
  name: 'ArticlesAdd',

  components: { quillEditor },
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      ruleForm: {
        title: '',
        articleBody: '',
        videoURL: ''
      }, // 新增的文章内容
      // 校验规则
      rules: {
        title: [
          { required: true, message: '文章标题', trigger: 'blur' },
          { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
        ],
        articleBody: [{ required: true, message: '请输入内容', trigger: 'change' }]
      },
      // 富文本校验规则
      editorOption: {
        modules: {
          toolbar: [
            ['bold', 'italic', 'underline', 'strike'], // 加粗，斜体，下划线，删除线
            ['blockquote', 'code-block'],
            [{ header: 1 }, { header: 2 }],
            [{ list: 'ordered' }, { list: 'bullet' }]
          ]
        }
      }
    }
  },
  methods: {
    // 确定执行
    queFn () {
      // 点击确定进行兜底校验
      this.$refs.ruleForm.validate(valid => {
        // 如果校验通过了
        if (valid) {
          console.log(this.ruleForm)
          this.$emit('FormDate', { ...this.ruleForm })
          // 重置表单 清空表单内容
          this.$refs.ruleForm.resetFields()
          this.ruleForm = {
            title: '',
            articleBody: '',
            videoURL: ''
          }
          this.$emit('update:dialogVisible', false)
        }
      })
    },
    // 取消执行
    quFn () {
      // 重置表单 清空表单内容
      this.$refs.ruleForm.resetFields()
      this.ruleForm = {
        title: '',
        articleBody: '',
        videoURL: ''
      }
      this.$emit('update:dialogVisible', false)
    },
    // 弹框关闭
    closeFn () {
      this.$refs.ruleForm.resetFields()
      this.ruleForm = {
        title: '',
        articleBody: '',
        videoURL: ''
      }
      this.$emit('update:dialogVisible', false)
    }
  }
}
</script>

<style >
.el-dialog {
  width: 800px;
}
.el-dialog__footer {
  text-align: right !important;
}
/* 富文本编辑器的高度 */
.ql-editor {
  height: 250px;
}
</style>
